package com.example.freshmenapp;

import androidx.appcompat.app.AppCompatActivity;

import androidx.annotation.Nullable;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.freshmenapp.Bean.LoginBean;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import android.os.Bundle;

public class LoginActivity extends AppCompatActivity {
    private EditText EtUserName,EtPassword;
    private TextView tvRegister;
    private Button btLogin;
    private String username;
    private String password;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState ) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();

    }

    private void initView() {
        EtUserName = findViewById(R.id.et_username);
        EtPassword = findViewById(R.id.et_password);
        btLogin = findViewById(R.id.bt_login);
        tvRegister = findViewById(R.id.tv_register);
//获取用户名
        Intent intent = getIntent();
        String Iname = intent.getStringExtra("name");
        EtUserName.setText(Iname);

        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,RegisterActivity.class));
            }
        });
        btLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
            }
        });
    }

    private void initData() {
        username = EtUserName.getText().toString() +"";
        password = EtPassword.getText().toString() +"";

        JSONObject jsonObject = new JSONObject();
        //将获取贷用户名和密码通过json形式传给服务端
        try {
            jsonObject.put("name",username);//用户名
            jsonObject.put("password",password);//密码
        } catch (JSONException e) {
            e.printStackTrace();
        }


        //建
        RequestQueue requestQueue = Volley.newRequestQueue(LoginActivity.this);

        //发起网络请求
        String url = "http://192.168.0.105:8080/freshmenapp/user/login";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, url,
                jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {

                Gson gson = new Gson();
                final LoginBean loginBean = gson.fromJson(jsonObject.toString(),LoginBean.class);
                final boolean success = loginBean.isSuccess();

                        if (success) {

                            startActivity(new Intent(LoginActivity.this,MainActivity.class));
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();

                        } else {
                            Toast.makeText(LoginActivity.this, "手机号码或密码错误", Toast.LENGTH_SHORT).show();
                        }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(LoginActivity.this,"请求失败",Toast.LENGTH_LONG).show();
            }
        });

        //讲请求内容嵌入队列中
        requestQueue.add(jsonObjectRequest);

    }
}